package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Dish;
import com.itheima.bean.Setmeal;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.exception.CustomException;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Tang
 * @date 2023/2/24
 */
@Slf4j
@RequestMapping("/setmeal")
@RestController
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐请求
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        //1.调用service的新增方法
        setmealService.saveWithDishs(setmealDto);
        //2.返回结果
        return R.success("新增成功！");
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R page(@RequestParam(defaultValue = "1") Integer page,
                  @RequestParam(defaultValue = "10") Integer pageSize, String name) {
        //1.调用service的分页查询
        Page<SetmealDto> setmealDtoPage = setmealService.findPage(page, pageSize, name);
        //2.返回结果
        return R.success(setmealDtoPage);
    }

    /**
     * 查询套餐列表
     *
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        //1.创建查询对象
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();

        //2.设置查询条件（根据分类id查询）
        wrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        wrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());

        //3.调用service查询方法
        List<Setmeal> list = setmealService.list(wrapper);

        //4.返回结果
        return R.success(list);
    }


    /**
     * @param status 套餐状态
     * @param ids 套餐id
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status,
                                  @RequestParam List<Long> ids) {
        List<Setmeal> setmeals = setmealService.listByIds(ids);
        setmeals.forEach(setmeal -> {
            setmeal.setStatus(status);
        });
        setmealService.updateBatchById(setmeals);
        return R.success("更新成功");
    }

    /**
     * @param ids 套餐ID
     * @return
     */
    @DeleteMapping
    public R<String> deleteBatch(@RequestParam List<Long> ids) {
        List<Setmeal> setmeals = setmealService.listByIds(ids);
        setmeals.forEach(setmeal -> {
            if (setmeal.getStatus() == 1) {
                throw new CustomException("售卖中的套餐不能删除");
            }
        });
        setmealService.removeByIds(ids);
        return R.success("更新成功");
    }
}
